package com.neusmart.tongrentang.activity;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.neusmart.tongrentang.R;
import com.neusmart.tongrentang.adapter.GuideAdapter;
import com.neusmart.tongrentang.constants.Key;
import com.neusmart.tongrentang.dataLoadRequest.DataLoadActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 * <p/>
 * keven
 */

public class GuideActivity extends DataLoadActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private LayoutInflater inflater;
    private ViewPager viewPager;
    private List<View> views = new ArrayList<>();
    private ImageView[] dots;

    private GuideAdapter adapter;
    private int currentIndex;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_guide;
    }

    @Override
    protected void init() {
        initView();
        setListener();
    }

    private void initView() {
        inflater = LayoutInflater.from(this);
        //保存引导页的集合
        int[] guideLayoutArray = {R.layout.guide_layout_1, R.layout.guide_layout_2,
                R.layout.guide_layout_3};
        for (int layoutId : guideLayoutArray)
            views.add(inflater.inflate(layoutId, null));
        viewPager = (ViewPager) findViewById(R.id.guide_view_pager);
        adapter = new GuideAdapter(views, this);
        viewPager.setAdapter(adapter);
        initDots();
    }

    private void initDots() {
        LinearLayout ll = (LinearLayout) findViewById(R.id.guide_dots);
        dots = new ImageView[views.size()];
        for (int i = 0; i < views.size(); i++) {
            View container = inflater.inflate(R.layout.guide_dot, null);
            ll.addView(container);
            dots[i] = (ImageView) container.findViewById(R.id.dot);
            dots[i].setEnabled(false);// 使能控件，如果设置为false，该控件永远不会活动，不管设置为什么属性，都无效；
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(true);//设置为true，表明激活该控件，控件处于活动状态，处于活动状态，就能响应事件了，比如触摸、点击、按键事件等；
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > views.size() - 1
                || currentIndex == position) {
            return;
        }
        dots[position].setEnabled(true);
        dots[currentIndex].setEnabled(false);
        currentIndex = position;
    }

    private void setListener() {
        viewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //
    }

    @Override
    public void onPageSelected(int position) {
        setCurrentDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guide_btn_enter_app://进入应用
                switch2Main();
                break;
            default:
                break;
        }
    }

    private void switch2Main() {
        setGuided();
        switchActivityAndFinish(MainActivity.class, null);
    }

    private void setGuided() {
        putBoolean(Key.IS_FIRST_IN, false);
    }

    @Override
    public void onBackPressed() {
        //
    }

}
